var mysql=require("mysql");

var conn_pool=null;

function connect_to_sql(host,port,db_name,uname,upwd){
	conn_pool=mysql.createPool({
	host:host,
	port:port,
	database:db_name,
	user:uname,
	password:upwd});
}

let query = function( sql, values ) {
  // 返回一个 Promise
  return new Promise(( resolve, reject ) => {
    conn_pool.getConnection(function(err, conn) {
      if (err) {
        reject(err);
      } else {
        conn.query(sql, values, (err, result) => {

          if (err) {
            reject(err);
          } else {
            resolve(result);
          }
          // 结束会话
          conn.release();
        })
      }
    })
  });
};


//获取用户数据
async function get_user_info_by_openId(openId){
	try{
		var result=await query("select * from user_info where openId =? limit 1",[openId]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//插入用户数据
async function insert_user_info(openId){
	try{
		var result=await query("insert into user_info(openId) values(?)",[openId]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

/*
//修改用户数据
async function edit_profile(id,nick,sex){
	try{
		var result=await query("update user_info set nick=?,sex=? where id=?",[nick,sex,id]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//游客是否存在
async function is_exist_guest(id){
	try{
		var result=await query("select count(*) from user_info where id=? and is_guest=1 and status=0",[id]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//是否存在此手机的验证操作
async function is_exist_phone(phone,opt_type){
	try{
		var result=await query("select count(*) from phone_chat where phone=? and opt_type=?",[phone,opt_type]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//是否手机号已经注册
async function is_phone_register(phone){
	try{
		//name就是数据库中的用户帐号，也是手机号，是唯一的
		var result=await query("select count(*) from user_info where name=?",[phone]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//插入手机验证数据
async function insert_phone_validate(phone,opt_type,validate_code,end_time){
	try{
		var result=await query("insert into phone_chat(phone,opt_type,validate_code,end_time,count) values(?,?,?,?,1)",[phone,opt_type,validate_code,end_time]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//更新手机验证数据
async function update_phone_validate(phone,opt_type,validate_code,end_time){
	try{
		var result=await query("update phone_chat set validate_code=?,end_time=?,count=count+1 where phone=? and opt_type=?",[validate_code,end_time,phone,opt_type]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}


//验证游客绑定手机的验证码是否正确
async function check_phone_validate(phone,opt_type,validate_code,end_time){
	try{
		var result=await query("select count(*) from phone_chat where phone=? and opt_type=? and validate_code=? and end_time>?",[phone,opt_type,validate_code,end_time]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//游客绑定手机，更新游客为正式用户
async function update_guest_bind_phone(id,phone,password){
	try{
		var result=await query("update user_info set name=?,password=?,is_guest=0 where id=?",[phone,password,id]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//获取正式用户数据
async function get_user_info_by_name_pwd(name,password){
	try{
		var result=await query("select id,nick,sex,face,vip,is_guest,status from user_info where name =? and password=? limit 1",[name,password]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//插入用户数据,phone就是name帐号名
async function insert_user_info(nick,phone,password,sex,face){
	try{
		var result=await query("insert into user_info(nick,name,password,sex,face,is_guest) values(?,?,?,?,?,0)",[nick,phone,password,sex,face]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

//更新用户密码数据
async function update_user_password(phone,password){
	try{
		var result=await query("update user_info set password=? where name=?",[password,phone]);
		return {err:0,result};
	}
	catch(err){
		return {err:1};
	}
}

*/
module.exports={
	connect:connect_to_sql,
	get_user_info_by_openId:get_user_info_by_openId,
	insert_user_info:insert_user_info,
	//edit_profile:edit_profile,
	//is_exist_guest:is_exist_guest,
	//is_exist_phone:is_exist_phone,
	//is_phone_register:is_phone_register,
	//insert_phone_validate:insert_phone_validate,
	//update_phone_validate:update_phone_validate,
	//check_phone_validate:check_phone_validate,
	//update_guest_bind_phone:update_guest_bind_phone,
	//get_user_info_by_name_pwd:get_user_info_by_name_pwd,
	//insert_user_info:insert_user_info,
	//update_user_password:update_user_password,
};
